<template>
  <basic-dialog
    width="65%"
    :title="dialogTitle"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
  >
    <BasicTable
      :tableHeight="400"
      :showSelectCol="false"
      :showPagination="false"
      :data-source="tableInfo.list"
      :columns="tableInfo.columns"
      :loading="tableInfo.loading"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicTable from '@/components/BasicTable';
import { STOCK_RELATIVE_OPTIONS } from './constants';
export default {
  components: { BasicDialog, BasicTable },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    handleSearch: {
      type: Function,
      default: () => {}
    },
    stockKey: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      dialogTitle: '',
      tableInfo: {
        list: [],
        columns: [],
        loading: false
      },
      searchData: {}
    }
  },
  watch: {
    visible (val) {
      if (!val) return
      this.$nextTick(() => {
        this.initStockInfo();
      });
    }
  },
  methods: {
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    initStockInfo () {
      const { title, columns } = STOCK_RELATIVE_OPTIONS[this.stockKey] ?? {};
      this.dialogTitle = title;
      this.tableInfo.columns = columns;
      this.getTableData();
    },
    getSearchParams (data) {
      // businessType 1, "采购订单" 2, "转货订单"
      const { businessType } = STOCK_RELATIVE_OPTIONS[this.stockKey] ?? {};
      this.searchData = { ...data, businessType };
    },
    async getTableData () {
      const { method } = STOCK_RELATIVE_OPTIONS[this.stockKey] ?? {};
      if (!method) return;
      const loadingFn = state => { this.tableInfo.loading = state };
      const retData = await method(this.searchData, loadingFn);
      if (retData) {
        this.tableInfo.list = retData;
      }
    }
  }
}
</script>
